package com.ourhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ourhealth.entity.Result;
import com.ourhealth.mapper.MemberMapper;
import com.ourhealth.mapper.OrderSettingMapper;
import com.ourhealth.mapper.OrdersMapper;
import com.ourhealth.mapper.SetMealMapper;
import com.ourhealth.pojo.Member;
import com.ourhealth.pojo.Order;
import com.ourhealth.pojo.Setmeal;
import com.ourhealth.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dddddh
 */
@Service(interfaceClass = OrderService.class)
@Transactional(rollbackFor = RuntimeException.class)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Override
    public Result order(Map map) {
        String orderDate = (String) map.get("orderDate");
        //判断该日期是否能预约
        Integer orderSettingByDay = orderSettingMapper.getOrderSettingByDay(orderDate);
        //乐观锁
        int version = orderSettingMapper.getVersionByDate(orderDate);
        if (orderSettingByDay>0){

                //检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
                String telephone = (String) map.get("telephone");
                Integer isMember = memberMapper.isMemberByPhone(telephone);
                if (isMember==0){
                    //注册会员
                    Member member = new Member();
                    member.setName((String) map.get("name"));
                    member.setPhoneNumber(telephone);
                    member.setIdCard((String) map.get("idCard"));
                    member.setSex((String) map.get("sex"));
                    member.setRegTime(new Date());
                    memberMapper.add(member);
                }
                Long memberId = ordersMapper.findMemberIdByPhone(telephone);
                String orderType = "微信预约";
                String orderStatus = "未到诊";

                //判断该用户是否在同一天预约了同一个套餐
                String setmealId = (String) map.get("setmealId");
                Integer isSameSetMeal = ordersMapper.isSameSetMealById(setmealId,memberId);
                if (isSameSetMeal==0){
                //添加订单
                ordersMapper.addOrder(memberId,orderDate,orderType,orderStatus,setmealId);
                //预约人数+1,
                int i = orderSettingMapper.editReservations(orderDate,version);

                //如果失败，抛异常回滚
                if (i<1){
                    throw new RuntimeException();
                }
                //将预约详情返回
                Order order = ordersMapper.findOrder(memberId,orderDate,setmealId);
                return new Result(true,"预约成功",order);
            }
            return new Result(false,"已经预约该检查");
        }
        return new Result(false,"该日期无法预约");
    }

    @Override
    public Map findById(Integer id) {
        Order order = ordersMapper.findById(id);
        Member member = memberMapper.getMemberById(order.getMemberId());
        Setmeal setmeal = setMealMapper.findById(order.getSetmealId());
        Map<String,String>  map = new HashMap<>();
        map.put("orderDate",order.getOrderDate().toString());
        map.put("orderType",order.getOrderType());
        map.put("orderStatus",order.getOrderStatus());
        map.put("member",member.getName());
        map.put("setmeal",setmeal.getName());
        return map;
    }
}
